//
//  NBAPlayer.swift
//  Sports
//
//  Created by tiger on 2017/10/14.
//  Copyright © 2017年 Meigabay. All rights reserved.
//

import UIKit
import SQLite

/// 球员位置
///
/// - pf: 大前锋－power forward
/// - sf: 小前锋－small forward
/// - sg: 得分后卫－shooting guard
/// - pg: 组织后卫－point guard
/// - c: 中锋－center
public enum NBAPlayerPosition: Int {
    case pf = 1
    case sf = 2
    case sg = 3
    case pg = 4
    case c = 5
    
    func getString() -> String {
        switch self {
        case .pf:
            return "大前锋"
        case .sf:
            return "小前锋"
        case .sg:
            return "得分后卫"
        case .pg:
            return "控球后卫"
        case .c:
            return "中锋"
        }
    }
}

//有部分自我决策能力的低端机器人
public class NBAPlayer: NBAWorker {
   
    //固定信息
    internal var position: NBAPlayerPosition = .c //场上位置
    internal var number: Int = 1 //球衣号码
    internal var clubId: String = "1" //俱乐部ID
    
    //打球能力
    internal var twoPoint: Double = 0.1 // 2分球命中率
    internal var threePoint: Double = 0.1 // 3分球命中率
    internal var controlAbility: Double = 0.3 // 控球能力
    internal var passAbility: Double = 0.3 // 传球能力
    internal var handlingStressAbility: Double = 0.5 //抗压能力
    
    //身体素质
    internal var bounceSpeed: Double = 0.1 //弹跳速度 米/秒
    internal var bounceHeight: Double = 0.7  //弹跳高度 米
    internal var runSpeed: Double = 10 //  奔跑速度 米/秒
    internal var thinkingSpeed: Double = 0.04 // 思维速度秒
    internal var physicalStrength: Double = 1 // 体力值 [0-1] 百分比
    
    private weak var game: NBAGame!
    private var brain: NBAPlayerBrain!
    private var behavior: NBAPlayerBehavior!
    
    public override init() {
        super.init()
        self.position = .c
        self.number = 1
        self.clubId = "1"
        
        self.twoPoint = 0.3
        self.threePoint  = 0.2
        self.controlAbility = 0.3
        self.passAbility = 0.3
        self.handlingStressAbility = 0.5
        
        self.bounceSpeed = 2
        self.bounceHeight = 0.7
        self.runSpeed  = 7.6
        self.thinkingSpeed = 0.04
        self.physicalStrength = 1
    }
    
    override init(result: Row) {
        let columns = NBAPlayerTableColumn()
        self.clubId = result[columns.clubId]
        self.number = result[columns.number]
        self.position = NBAPlayerPosition(rawValue: result[columns.position])!
        
        self.twoPoint = result[columns.twoPoint]
        self.threePoint = result[columns.threePoint]
        self.controlAbility = result[columns.controlAbility]
        self.passAbility = result[columns.passAbility]
        self.handlingStressAbility = result[columns.handlingStressAbility]
        
        self.bounceSpeed = result[columns.bounceSpeed]
        self.bounceHeight = result[columns.bounceHeight]
        self.runSpeed = result[columns.runSpeed]
        self.thinkingSpeed = result[columns.thinkingSpeed]
        self.physicalStrength = result[columns.physicalStrength]
        
        super.init(result: result)
    }
    

}

internal extension NBAPlayer {
    //开始工作
    func startWork(game: NBAGame) {
        self.game = game
        self.behavior = NBAPlayerBehavior(aPlayer: self)
        //分配脑子
        self.brain = NBAPlayerBrain.createBrain(aPlayer: self,behavior: self.behavior)
        self.brain.startWork()
    }
    
    func stopWork() {
        self.brain.stopWork()
    }
}

internal class NBAPlayerBehavior {
    
    weak var player: NBAPlayer!
    
    init(aPlayer: NBAPlayer) {
        self.player = aPlayer
    }
    
    //MARK: - 移动
    func moveTo(coordinate: NBAWorkerCoordinate) {
        
    }
    
    //MARK: - 三分
    //普通三分
    //干拔三分
    //logo三分
    
    //MARK: - 两分
    //三步上篮两分
    //欧洲步两分
    //抛投两分
    //跳投两分
    //勾手两公
    //补扣两分
    //扣篮两分
    //双手灌篮两分
}

internal class NBAPlayerBrain {
    weak var player: NBAPlayer!
    weak var behavior: NBAPlayerBehavior!
    /// 脑子工作队列
    private lazy var workingQueue: DispatchQueue = DispatchQueue(label: String(format: "q_%0.0f",NSDate().timeIntervalSince1970*1000))
    
    init(aPlayer: NBAPlayer,behavior: NBAPlayerBehavior) {
        self.player = aPlayer
        self.behavior = behavior
    }
    
    class func createBrain(aPlayer: NBAPlayer,behavior: NBAPlayerBehavior) -> NBAPlayerBrain {
        var brain: NBAPlayerBrain!
        switch aPlayer.position {
        case .c:
            brain = NBAPlayerBrainC(aPlayer: aPlayer, behavior: behavior)
        case .pg:
            brain = NBAPlayerBrainPG(aPlayer: aPlayer, behavior: behavior)
        case .sg:
            brain = NBAPlayerBrainSG(aPlayer: aPlayer, behavior: behavior)
        case .sf:
            brain = NBAPlayerBrainSF(aPlayer: aPlayer, behavior: behavior)
        case .pf:
            brain = NBAPlayerBrainPF(aPlayer: aPlayer, behavior: behavior)
        }
        return brain
    }
    
    fileprivate func startWork() {
        //开始工作咯
        self.addObservers()
    }
    
    fileprivate func stopWork() {
        self.removeObservers()
    }
    
    private func addObservers() {
        NotificationCenter.default.addObserver(self, selector: #selector(handNotification(notification:)), name: NSNotification.Name.NBAGameWhistle, object: nil)
    }
    
    private func removeObservers() {
        NotificationCenter.default.removeObserver(self)
    }
    
    @objc private func handNotification(notification: NSNotification) {
        if let whistle = notification.object as? NBARefereeWhistle {
            switch(whistle.type) {
            case .start:
                self.jumpBall()
                break
            case .end:
                self.end()
                break
            case .pause:
                self.thinkingWhenPause()
                break
            case .resume:
                self.thinking()
                break
            case .jump:
                self.jumpBall()
                break;
            default:
                break
            }
        }
    }
    
    //入场
    func intoTheArena() {
        
    }
    
    func jumpBall() {
        //
    }
    
    func thinking() {
        
    }
    
    func end() {
        
    }
    
    func thinkingWhenPause() {
        
    }
}

/// 中锋头脑
internal class NBAPlayerBrainC: NBAPlayerBrain {
    
    override func intoTheArena() {
        //上中圈
    }
    
    override func jumpBall() {
        
    }
    
    override func thinking() {
        
    }
    
    override func end() {
        
    }
    
    override func thinkingWhenPause() {
        
    }
}

/// 大前锋头脑
internal class NBAPlayerBrainPF: NBAPlayerBrain {
    override func intoTheArena() {
        //上中圈
        
    }
    
    override func jumpBall() {
        
    }
    
    override func thinking() {
        
    }
    
    override func end() {
        
    }
    
    override func thinkingWhenPause() {
        
    }
}

/// 小前锋头脑
internal class NBAPlayerBrainSF: NBAPlayerBrain {
    override func intoTheArena() {
        //上中圈
    }
    
    override func jumpBall() {
        
    }
    
    override func thinking() {
        
    }
    
    override func end() {
        
    }
    
    override func thinkingWhenPause() {
        
    }
}

/// 控球后卫头脑
internal class NBAPlayerBrainSG: NBAPlayerBrain {
    override func intoTheArena() {
        //上中圈
    }
    
    override func jumpBall() {
        
    }
    
    override func thinking() {
        
    }
    
    override func end() {
        
    }
    
    override func thinkingWhenPause() {
        
    }
}

/// 得分后卫头脑
internal class NBAPlayerBrainPG: NBAPlayerBrain {
    override func intoTheArena() {
        //上中圈
    }
    
    override func jumpBall() {
        
    }
    
    override func thinking() {
        
    }
    
    override func end() {
        
    }
    
    override func thinkingWhenPause() {
        
    }
}

